import logging
import requests
from requests.auth import HTTPBasicAuth


class ProxyMiddleware(object):
    def __init__(self, proxy_url, auth_user, auth_pass):
        self.logger = logging.getLogger(__name__)
        self.proxy_url = proxy_url
        self.auth_user = auth_user
        self.auth_pass = auth_pass

    def get_random_proxy(self):
        try:
            if self.auth_user == '' or self.auth_pass == '':
                response = requests.get(self.proxy_url)
            else:
                auth = HTTPBasicAuth(self.auth_user, self.auth_pass)
                response = requests.get(self.proxy_url, auth=auth)
            if response.status_code == 200:
                proxy = response.text
                return proxy
        except requests.ConnectionError:
            self.logger.debug('get proxy error %d' % response.status_code)
            return False

    def process_request(self, request, spider):
        proxy = self.get_random_proxy()
        if proxy:
            uri = 'https://{proxy}'.format(proxy=proxy)
            self.logger.debug('use proxy ' + proxy)
            request.meta['proxy'] = uri

    @classmethod
    def from_crawler(cls, crawler):
        settings = crawler.settings
        return cls(
            proxy_url=settings.get('PROXY_URL'),
            auth_user=settings.get('PROXY_BASIC_AUTH_USER'),
            auth_pass=settings.get('PROXY_BASIC_AUTH_PASS'),
        )
